<template>
	<view class="tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed customBack :bottomShadow="false" :backgroundColor="navBarBackgroundColor" id="navbar">
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack" :style="[navBarStyle]">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-discover-fill'></text>
			</view>
			<view class="tn-flex tn-flex-col-center tn-flex-row-center" :style="[navBarStyle2]">
				<view class="tn-text-bold tn-text-xl">{{data.title}}</view>
			</view>
		</tn-nav-bar>

		<swiper class="card-swiper" :circular="true" :autoplay="true" duration="500" interval="22000">
			<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
				<view class="swiper-item image-banner">
					<image :src="item.url" mode="widthFix" style="position: absolute;top: -80px;"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="indication">
			<block v-for="(item,index) in swiperList" :key="index">
				<view class="spot" :class="cardCur==index?'active':''"></view>
			</block>
		</view>

		<!-- 头部start-->
		<view class="shop-function tn-margin-bottom-xl" style="padding-top: 80px">
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin">
				<view class="justify-content-item">
					<view class="tn-flex tn-flex-col-center tn-flex-row-left">
						<view class="tn-color-white" style="width: 90vw;">
							<view class="tn-padding-right tn-text-xxl tn-text-bold">
								{{data.title}}
							</view>
							<view class="tn-padding-right tn-padding-top-sm tn-text-ellipsis tn-text-sm">
								<rich-text class="tn-padding-right-sm" :nodes="data.content"></rich-text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 头部 end-->

		<tn-modal v-model="show" :custom="true" showCloseBtn>
			<view class="custom-modal-content">
				<image @tap="previewQRCodeImage" :src='preUrl' mode='aspectFill' style="width: 100%;height: 300px;"></image>
				<tn-button backgroundColor="#fbbd12" padding="40rpx 0" width="100%" shadow fontBold @click="fenxiang(preUrl)">
				  <text class="tn-icon-send tn-padding-right-xs tn-color-white"></text>
				  <text class="tn-color-white">分 享</text>
				</tn-button>
			</view>
		</tn-modal>

		<view class="group-wrap tn-bg-white" id="page_tips">


			<view class="" style="padding: 30rpx 20rpx;">
				<tn-waterfall ref="waterfall" v-model="list" @finish="handleWaterFallFinish">
					<template v-slot:left="{ leftList }">
						<view v-for="(item, index) in leftList" :key="index" class="product__item"
							@click="showModal(item.url)">
							<view class="item__image">
								<tn-lazy-load :threshold="6000" height="100%" :image="item.url" :index="item.id"
									imgMode="widthFix"></tn-lazy-load>
							</view>
						</view>
					</template>
					<template v-slot:right="{ rightList }">
						<view v-for="(item, index) in rightList" :key="index" class="product__item"
							@click="showModal(item.url)">
							<view class="item__image">
								<tn-lazy-load :threshold="6000" height="100%" :image="item.url" :index="item.id"
									imgMode="widthFix"></tn-lazy-load>
							</view>
						</view>
					</template>
				</tn-waterfall>
			</view>
			<tn-load-more :status="loadStatus"></tn-load-more>
		</view>
	</view>
</template>

<script>
	import template_page_mixin, {
		data
	} from '@/uni_modules/tuniao-ui/libs/mixin/template_page_mixin.js'
	import {
		listWeddingPhoto,
		getWeddingPage
	} from '@/api/common/api'
	export default {
		name: 'wallpaper',
		mixins: [template_page_mixin],
		data() {
			return {
				show: false,
				swiperList: [],
				/* 导航*/
				navBarRectInfo: {},
				navBarChangebaseLineHeight: 0,
				navBarStyle: {
					color: '#FFFFFF',
					opacity: 1,
					display: 'flex'
				},
				navBarStyle2: {
					color: 'rgba(255,255,255,0)',
					opacity: 1,
					display: 'flex'
				},
				navBarBackgroundColor: 'rgba(255, 255, 255, 0)',
				activeBgAnimation: {},

				/* 瀑布流*/
				loadStatus: 'loadmore',
				list: [],
				data: {
					title: '',
					content: ''
				},
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					typeId: undefined
				},
				pageId: '',
				preUrl: '',
			}
		},

		onLoad(e) {
			this.loading=true;
			console.log(e)
			this.queryParams.typeId = e.typeId
			this.pageId = e.id
			this.getRandomData()
			this.initType()
		},
		onReachBottom() {
			if(this.loadStatus != 'normal'){
				this.loading=true;
				this.queryParams.pageNum = this.queryParams.pageNum + 1
				this.getRandomData()
			}
		},
		onReady() {
			this.$nextTick(() => {
				this.initNavBarRectInfo()
			})
		},
		onPageScroll() {
			this.updateNavBarRectInfo()
		},

		methods: {
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},

			// cardSwiper
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			// 初始化导航栏信息
			async initNavBarRectInfo() {
				const navBarRectInfo = await this._tGetRect('#navbar')
				const pageTipsRectInfo = await this._tGetRect('#page_tips')
				// console.log(navBarRectInfo, pageTipsRectInfo, navBarRectInfo?.top, pageTipsRectInfo?.top);
				if (!navBarRectInfo.hasOwnProperty('top') || !pageTipsRectInfo.hasOwnProperty('top')) {
					setTimeout(() => {
						this.initNavBarRectInfo()
					}, 10)
					return
				}
				this.navBarRectInfo = {
					top: navBarRectInfo.top
				}
				this.navBarChangebaseLineHeight = pageTipsRectInfo.top - navBarRectInfo.top
			},
			// 更新导航栏信息
			updateNavBarRectInfo() {
				this._tGetRect('#page_tips').then((res) => {
					const top = res?.top || 0
					if (!top) {
						return
					}
					const differHeight = top - this.navBarRectInfo.top
					const opacity = differHeight / this.navBarChangebaseLineHeight
					if (opacity < 0) {
						// this.navBarStyle.opacity = 1
						// this.navBarStyle.display = 'flex'
						this.navBarStyle.color = 'rgba(0, 0, 0, ${opacity})'
						this.navBarStyle2.color = 'rgba(0, 0, 0, ${opacity})'
						this.navBarBackgroundColor = `rgba(255, 255, 255, 1)`
					} else {
						// this.navBarStyle.opacity = 1 - opacity
						// this.navBarStyle.display = 'flex'
						this.navBarStyle.color = 'rgba(255, 255, 255, 1)'
						this.navBarStyle2.color = 'rgba(255, 255, 255, 0)'
						this.navBarBackgroundColor = `rgba(255, 255, 255, ${1 - opacity})`
					}

					// console.log(top, differHeight, opacity);
				})
			},
			/* 瀑布流*/
			// 获取随机数据
			getRandomData() {
				this.loadStatus = 'loading'
				listWeddingPhoto(this.queryParams).then(response => {
					if (response.rows.length > 0) {
						response.rows.forEach(item => {
							
							this.list.push(item);
						})
					} else {
						this.loadStatus = 'nomore'
					}
				});
			},
			// 瀑布流加载完毕事件
			handleWaterFallFinish() {
				this.loadStatus = 'loadmore'
			},
			initType() {
				getWeddingPage(this.pageId).then(res => {
					this.data.title = res.data.title;
					this.data.content = res.data.content;
					const swiper = {
						id: 1,
						url: res.data.pic
					}
					this.swiperList.push(swiper)
				})
			},
			// 弹出模态框
			showModal(url) {
				// this.preUrl = url
				// this.openModal()
				uni.navigateTo({
					url: '../photo/photo?url='+url,
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.03);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		// color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.05;
			background-color: #000000;
		}
	}

	/* 内容 start*/
	.tn-plan-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 内容 end*/

	/* 轮播视觉差 start */
	.card-swiper {
		height: 500rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		padding: 0rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		will-change: transform;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
		will-change: transform;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		opacity: 0;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -60rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}

	/* 顶部 */
	.shop-function {
		position: relative;
		z-index: 1;
		margin-top: -450rpx;
		padding-bottom: 110rpx;
		background-image: linear-gradient(rgba(255, 255, 255, 0.01), rgba(0, 0, 0, 0.4));
	}

	

	/* 内容*/
	.group-wrap {
		position: relative;
		z-index: 1;
		// padding: 20rpx 30rpx;
		margin-top: -130rpx;
		margin-bottom: 40rpx;
		border-radius: 30rpx 30rpx 0 0;
	}

	.tn-tabbar-height {
		min-height: 20rpx;
		height: calc(40rpx + env(safe-area-inset-bottom) / 2);
	}


	/* 瀑布流*/
	.product__item {
		background-color: #FFFFFF;
		overflow: hidden;
		margin: 0 10rpx;
		margin-bottom: 20rpx;
		// box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);

		.item {

			/* 图片 start */
			&__image {
				width: 100%;
				height: auto;
				background-color: #FFFFFF;
				border: 1rpx solid #F8F7F8;
				border-radius: 10rpx;
				overflow: hidden;
			}

			/* 图片 end */
		}
	}
</style>
